package ssl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

public class LoadCert {
    public static void main(String[] args) throws Exception {

        https("https://47.89.244.232/bitcoin/account");
    }

	private static void https(String url) throws FileNotFoundException, KeyStoreException, IOException, NoSuchAlgorithmException,
			CertificateException, NoSuchProviderException, KeyManagementException, MalformedURLException {
		X509TrustManager sunJSSEX509TrustManager;
        // 加载 Keytool 生成的证书文件
        char[] passphrase;
        String p = "chipsmatrix";
        passphrase = p.toCharArray();
//        File file = new File("E:\\Program Files\\Java\\jdk1.8.0_162\\jre\\lib\\security\\matrix");
        File file = new File("E:\\code\\inchat_parent_develop_2\\chips-gateway\\chips-gateway-client\\chips-gateway-client-wallet\\src\\main\\resources\\matrix");
        //File file = new File("matrix");
        System.out.println("Loading KeyStore " + file + "...");
        InputStream in = new FileInputStream(file);
        //InputStream in = CommonUtil.getHttpsCa() ;
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(in, passphrase);
        in.close();
        // 构造 javax.net.ssl.TrustManager 对象
        TrustManagerFactory tmf =
        TrustManagerFactory.getInstance("SunX509", "SunJSSE");
        tmf.init(ks);
        TrustManager tms [] = tmf.getTrustManagers();
        
       /* System.out.println(KeyStore.getDefaultType().toString());
        System.out.println(TrustManagerFactory.getDefaultAlgorithm().toString());*/
        
        // 使用构造好的 TrustManager 访问相应的 https 站点
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tms, new java.security.SecureRandom());
        SSLSocketFactory ssf = sslContext.getSocketFactory();
//        URL myURL = new URL("https://47.254.39.140/ethereum/account");
        URL myURL = new URL(url);
        HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
        httpsConn.setSSLSocketFactory(ssf);
        InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
        int respInt = insr.read();
        while (respInt != -1) {
            System.out.print((char) respInt);
            respInt = insr.read();
        }
	}
    
    
}
